package my.service;

import com.anna.config.Ioc.Service;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import my.model.Message;

import java.util.List;

/**
 * Created by Administrator on 2017/4/20 0020.
 */
@Service
public class MessageService {

    public static List<Message> getAllMessageDetail() {
        return Message.dao.find("SELECT * FROM message_detail_view");
    }

    public static Page<Message> paginateAllMessageDetail(int page, int pageSize) {
        Page<Message> messagePage = Message.dao.paginate(page, pageSize, "SELECT *", "FROM message_detail_view");
        return messagePage;
    }

    public static boolean deleteMessageById(int userId, int id) {
        boolean ret = true;
        try {
            Db.update("DELETE FROM message WHERE user_id = ? AND id = ?", userId, id);
        } catch (Exception e) {
            e.printStackTrace();
            ret = false;
        }
        return ret;
    }

    public static boolean updateMessage(Message message) {
        boolean ret = false;
        try {
            message.update();
            ret = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ret;
    }

    public static Message getMessageById(int id) {
        return Message.dao.findFirst("SELECT * FROM message WHERE id = ?", id);
    }
}
